/**
 * com.mantisframework.sys.action
 *

 */

package com.mantisframework.sys.action;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import jodd.util.StringUtil;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.mantisframework.core.enu.DepartmentTypeEnum;
import com.mantisframework.core.page.PageBean;
import com.mantisframework.core.struts.BaseAction;
import com.mantisframework.model.MSysDepartment;
import com.mantisframework.model.MSysUsers;
import com.mantisframework.sys.enumbean.DeptSearchFields;
import com.mantisframework.sys.enumbean.UserSearchFields;
import com.mantisframework.sys.service.ISysDepartmentService;
import com.opensymphony.xwork2.ActionContext;

/**
 * 
 * 
 * 
 * @author 李奎和
 * 
 *         <p>
 *         时间：2013-1-13 下午07:07:18
 *         </p>
 * 
 *         <p>
 *         电子邮件：likh@hwttnet.com
 *         </p>
 */
@Scope("prototype")
@Controller("sysDeptsAction")
public class SysDeptsAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private static Logger _log = Logger.getLogger(SysDeptsAction.class);
	private static final String LIST = "list";

	@Resource
	private ISysDepartmentService sysDepartmentService;

	private DepartmentTypeEnum[] deptType = DepartmentTypeEnum.values();

	private String op;
	private MSysDepartment dept;
	private String deptBrief = "";

	public String add() {
		if ("add".equals(op)) {
			dept = new MSysDepartment();
			return "add2";
		} else if ("save".equals(op) || "update".equals(op)) {
			byte[] brief = deptBrief.getBytes();
			dept.setDeptBrief(brief);
			boolean flag = false;
			flag = this.sysDepartmentService.save(dept);
			if (flag)
				return ajaxForwardSuccess("操作成功");
			else
				return ajaxForwardError("操作失败");
		} else if ("modify".equals(op)) {
			dept = sysDepartmentService.getPojoById(dept.getDeptId());
			deptBrief = new String(dept.getDeptBrief());
			return "add2";
		}
		return SUCCESS;
	}

	public String list() {
		try {
			int pageNum = this.getPageNum() > 0 ? this.getPageNum() : 1;
			if (this.getNumPerPage() <= 0) {
				this.setNumPerPage(10);
			}
			Map<DeptSearchFields, String> criterias = getCriterias();
			// PageBean pageBean = sysUsersService.querySysUsers(criterias,
			// new PageBean(pageNum, getNumPerPage()));
			PageBean pageBean = sysDepartmentService.querySysDepts(criterias,
					new PageBean(pageNum, getNumPerPage()),
					getOrderDirection(), getOrderField());
			ActionContext.getContext().put("deptList", pageBean.getDatas());
			this.setTotalCount(pageBean.getTotalRecord());
			this.setTotalPage(pageBean.getTotalPage());
		} catch (Exception e) {
			_log.error(e.getMessage(), e);
		}
		return LIST;
	}

	private Map<com.mantisframework.sys.enumbean.DeptSearchFields, String> getCriterias() {
		Map<DeptSearchFields, String> criterias = new HashMap<DeptSearchFields, String>();
		if (dept != null) {
			if (StringUtil.isNotEmpty(dept.getDeptName())) {
				criterias.put(DeptSearchFields.deptName, dept.getDeptName());
			}
			if (StringUtil.isNotEmpty(dept.getDeptType())) {
				criterias.put(DeptSearchFields.deptType, dept.getDeptType());
			}
		}

		return criterias;
	}

	public String view() {
		dept = this.sysDepartmentService.getPojoById(dept.getDeptId());
		byte[] brief = dept.getDeptBrief();
		if (null != brief && brief.length > 0)
			deptBrief = new String(brief);
		return SUCCESS;
	}

	public String delete() {
		try {
			boolean flag = false;
			flag = this.sysDepartmentService.deleteDeptById(dept.getDeptId());
			if (flag)
				return ajaxForwardSuccess("删除成功");
			else
				return ajaxForwardError("删除失败");
		} catch (Exception e) {
			_log.error(e.getMessage(), e);
			return ajaxForwardError("删除失败");
		}
	}

	/**
	 * @return sysDepartmentService
	 */

	public ISysDepartmentService getSysDepartmentService() {
		return sysDepartmentService;
	}

	/**
	 * @param sysDepartmentService
	 *            要设置的 sysDepartmentService
	 */

	public void setSysDepartmentService(
			ISysDepartmentService sysDepartmentService) {
		this.sysDepartmentService = sysDepartmentService;
	}

	/**
	 * @return op
	 */

	public String getOp() {
		return op;
	}

	/**
	 * @param op
	 *            要设置的 op
	 */

	public void setOp(String op) {
		this.op = op;
	}

	/**
	 * @return dept
	 */

	public MSysDepartment getDept() {
		return dept;
	}

	/**
	 * @param dept
	 *            要设置的 dept
	 */

	public void setDept(MSysDepartment dept) {
		this.dept = dept;
	}

	/**
	 * @return deptType
	 */

	public DepartmentTypeEnum[] getDeptType() {
		return deptType;
	}

	/**
	 * @param deptType
	 *            要设置的 deptType
	 */

	public void setDeptType(DepartmentTypeEnum[] deptType) {
		this.deptType = deptType;
	}

	/**
	 * @return deptBrief
	 */

	public String getDeptBrief() {
		return deptBrief;
	}

	/**
	 * @param deptBrief
	 *            要设置的 deptBrief
	 */

	public void setDeptBrief(String deptBrief) {
		this.deptBrief = deptBrief;
	}
}
